{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Token Counting Handler\n",
    "\n",
    "This notebook walks through how to use the TokenCountingHandler and how it can be used to track your prompt, completion, and embedding token usage over time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tiktoken\n",
    "from llama_index.llms import Anthropic\n",
    "\n",
    "from llama_index import (\n",
    "    SimpleDirectoryReader,\n",
    "    VectorStoreIndex,\n",
    "    ServiceContext,\n",
    "    set_global_service_context,\n",
    ")\n",
    "from llama_index.callbacks import CallbackManager, TokenCountingHandler\n",
    "\n",
    "import os\n",
    "\n",
    "os.environ[\"ANTHROPIC_API_KEY\"] = \"YOUR_API_KEY\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Here, we setup the callback and the serivce context. We set a global service context so that we don't have to worry about passing it into indexes and queries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/loganmarkewich/llama_index/llama-index/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "token_counter = TokenCountingHandler(\n",
    "    tokenizer=tiktoken.encoding_for_model(\"gpt-3.5-turbo\").encode\n",
    ")\n",
    "\n",
    "callback_manager = CallbackManager([token_counter])\n",
    "\n",
    "llm = Anthropic()\n",
    "\n",
    "service_context = ServiceContext.from_defaults(\n",
    "    llm=llm, callback_manager=callback_manager, embed_model=\"local\"\n",
    ")\n",
    "\n",
    "# set the global default!\n",
    "set_global_service_context(service_context)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Token Counting\n",
    "\n",
    "The token counter will track embedding, prompt, and completion token usage. The token counts are __cummulative__ and are only reset when you choose to do so, with `token_counter.reset_counts()`.\n",
    "\n",
    "### Embedding Token Usage\n",
    "\n",
    "Now that the service context is setup, let's track our embedding token usage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = SimpleDirectoryReader(\"../data/paul_graham\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorStoreIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16852\n"
     ]
    }
   ],
   "source": [
    "print(token_counter.total_embedding_token_count)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That looks right! Before we go any further, lets reset the counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "token_counter.reset_counts()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LLM + Embedding Token Usage\n",
    "\n",
    "Next, let's test a query and see what the counts look like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(similarity_top_k=4)\n",
    "response = query_engine.query(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Embedding Tokens:  8 \n",
      " LLM Prompt Tokens:  3527 \n",
      " LLM Completion Tokens:  214 \n",
      " Total LLM Token Count:  3741 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    \"Embedding Tokens: \",\n",
    "    token_counter.total_embedding_token_count,\n",
    "    \"\\n\",\n",
    "    \"LLM Prompt Tokens: \",\n",
    "    token_counter.prompt_llm_token_count,\n",
    "    \"\\n\",\n",
    "    \"LLM Completion Tokens: \",\n",
    "    token_counter.completion_llm_token_count,\n",
    "    \"\\n\",\n",
    "    \"Total LLM Token Count: \",\n",
    "    token_counter.total_llm_token_count,\n",
    "    \"\\n\",\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Token Counting + Streaming!\n",
    "\n",
    "The token counting handler also handles token counting during streaming.\n",
    "\n",
    "Here, token counting will only happen once the stream is completed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "token_counter.reset_counts()\n",
    "\n",
    "query_engine = index.as_query_engine(similarity_top_k=4, streaming=True)\n",
    "response = query_engine.query(\"What happened at Interleaf?\")\n",
    "\n",
    "# finish the stream\n",
    "for token in response.response_gen:\n",
    "    # print(token, end=\"\", flush=True)\n",
    "    continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Embedding Tokens:  6 \n",
      " LLM Prompt Tokens:  3631 \n",
      " LLM Completion Tokens:  214 \n",
      " Total LLM Token Count:  3845 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    \"Embedding Tokens: \",\n",
    "    token_counter.total_embedding_token_count,\n",
    "    \"\\n\",\n",
    "    \"LLM Prompt Tokens: \",\n",
    "    token_counter.prompt_llm_token_count,\n",
    "    \"\\n\",\n",
    "    \"LLM Completion Tokens: \",\n",
    "    token_counter.completion_llm_token_count,\n",
    "    \"\\n\",\n",
    "    \"Total LLM Token Count: \",\n",
    "    token_counter.total_llm_token_count,\n",
    "    \"\\n\",\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Advanced Usage\n",
    "\n",
    "The token counter tracks each token usage event in an object called a `TokenCountingEvent`. This object has the following attributes:\n",
    "\n",
    "- prompt -> The prompt string sent to the LLM or Embedding model\n",
    "- prompt_token_count -> The token count of the LLM prompt\n",
    "- completion -> The string completion received from the LLM (not used for embeddings)\n",
    "- completion_token_count -> The token count of the LLM completion (not used for embeddings)\n",
    "- total_token_count -> The total prompt + completion tokens for the event\n",
    "- event_id -> A string ID for the event, which aligns with other callback handlers\n",
    "\n",
    "These events are tracked on the token counter in two lists:\n",
    "\n",
    "- llm_token_counts\n",
    "- embedding_token_counts\n",
    "\n",
    "Let's explore what these look like!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Num LLM token count events:  1\n",
      "Num Embedding token count events:  1\n"
     ]
    }
   ],
   "source": [
    "print(\"Num LLM token count events: \", len(token_counter.llm_token_counts))\n",
    "print(\"Num Embedding token count events: \", len(token_counter.embedding_token_counts))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This makes sense! The previous query embedded the query text, and then made 2 LLM calls (since the top k was 4, and the default chunk size is 1024, two seperate calls need to be made so the LLM can read all the retrieved text).\n",
    "\n",
    "Next, let's quickly see what these events look like for a single event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "prompt:  user: Context information is below.\n",
      "---------------------\n",
      "a web app, is common now, but at the time  ...\n",
      "\n",
      "prompt token count:  3631 \n",
      "\n",
      "completion:  assistant:  Based on the context, a few key things happened at Interleaf:\n",
      "\n",
      "- It was a software compa ...\n",
      "\n",
      "completion token count:  199 \n",
      "\n",
      "total token count 3830\n"
     ]
    }
   ],
   "source": [
    "print(\"prompt: \", token_counter.llm_token_counts[0].prompt[:100], \"...\\n\")\n",
    "print(\n",
    "    \"prompt token count: \", token_counter.llm_token_counts[0].prompt_token_count, \"\\n\"\n",
    ")\n",
    "\n",
    "print(\"completion: \", token_counter.llm_token_counts[0].completion[:100], \"...\\n\")\n",
    "print(\n",
    "    \"completion token count: \",\n",
    "    token_counter.llm_token_counts[0].completion_token_count,\n",
    "    \"\\n\",\n",
    ")\n",
    "\n",
    "print(\"total token count\", token_counter.llm_token_counts[0].total_token_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
